package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("adminOrderController")
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 订单的条件分页查询
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    public Result list(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单的条件分页查询，接收到的前端的参数为{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.list(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查看订单的详细信息
     *
     * @param id 订单id
     * @return
     */
    @GetMapping("/details/{id}")
    public Result getDetails(@PathVariable Long id) {
        log.info("查看订单的详细信息，参数为订单的id：{}", id);
        OrderVO orderVO = orderService.getDetailsById(id);
        return Result.success(orderVO);
    }


    /**
     * 计算各个状态订单的数量
     *
     * @return
     */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> countOrders() {
        log.info("计算各个状态订单的数量");
        OrderStatisticsVO orderStatisticsVO = orderService.countOrders();
        log.info("计算出各个状态订单的数量并返回给前端的数据为：{}", orderStatisticsVO);
        return Result.success(orderStatisticsVO);
    }

    /**
     * 取消订单
     *
     * @param ordersCancelDTO
     * @return
     */
    @PutMapping("/cancel")
    public Result<OrdersCancelDTO> cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        log.info("取消订单，接收到的参数为：{}", ordersCancelDTO);
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 完成订单
     *
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Long id) {
        log.info("取消订单，参数为id：{}", id);
        orderService.complete(id);
        return Result.success();
    }

    /**
     * 商户接单
     *
     * @param ordersConfirmDTO
     * @return
     */
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("商户接单，参数为：{}", ordersConfirmDTO);
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 商户拒单
     *
     * @param ordersRejectionDTO
     * @return
     */
    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("商户拒单,参数为：{}", ordersRejectionDTO);
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 商家派送
     *
     * @param id
     * @return
     */
    @PutMapping("delivery/{id}")
    public Result delivery(@PathVariable Long id) {
        log.info("商家派送，参数为：{}", id);
        orderService.delivery(id);
        return Result.success();
    }


}
